package com.tem.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.tem.common.tool.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import com.tem.product.entity.SysCategory;
import com.api.manage.pojo.product.SysCategoryBean;
import com.tem.product.service.ISysCategoryService;
import com.tem.common.core.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 系统 前端控制器
 * </p>
 * @author 陈辉
 * @since 2023-11-08
 */
@RestController
@RequestMapping("/product/sysCategory")
public class SysCategoryController extends BaseController{
    @Autowired
    private ISysCategoryService sysCategoryService;

    @ApiOperation(value = "系统列表")
    @RequestMapping("list")
    public ResultUtil<IPage<SysCategoryBean>> list(SysCategoryBean request, HttpServletRequest req){
        if(request.getSystemLevelId()==null){
            //request.setSystemLevelId(this.getSystemLevelId(req));
        }
        List<SysCategoryBean> responseList = sysCategoryService.treeBeans(request);
        Page page = new Page<>(request.getCurrent(),request.getSize(),responseList.size());
        page.setRecords(responseList);
        return ResultUtil.success(page);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增系统")
    public ResultUtil save(@RequestBody SysCategory entity){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        if(entity.getSysCategoryId()!=null){
            sysCategoryService.updateById(entity);
        }else{
            entity.setDeleted(0);
            sysCategoryService.save(entity);
        }
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询系统")
    public ResultUtil<SysCategoryBean> getById(@PathVariable String id){
        SysCategory entity = sysCategoryService.getById(id);
        if(entity!=null){
            SysCategoryBean bean = new SysCategoryBean();
            BeanUtils.copyProperties(entity,bean);
            bean.setCategoryPathName(sysCategoryService.getPathName(bean.getCategoryPath(),id));
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改系统")
    public ResultUtil update(@RequestBody SysCategory entity){
        sysCategoryService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除系统")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        sysCategoryService.removeById(id);
        return ResultUtil.success();
    }
}